
function [sumsquares] = mom(thet,poids,moments)

disp(' ')
disp('using program mom to compute sum of squares')


% retransform the parameters related to correlations to have them sit
% in the 0,1 range
thet(2) = 1/(1+exp(-thet(2)));
thet(5) = 1/(1+exp(-thet(5)));

thet(1) = abs(thet(1));
thet(3) = abs(thet(3));
thet(4) = abs(thet(4));

% Compute the weighted distance between certain simulated and observed

% this will house the five simulated moments
moments_sim = zeros(5,1);

% set up model parameters

whatinfo = 2;
% whatinfo = 1 implies "complete information" solution as defined in the paper; whatinfo = 2 implies "learning" solution 
answinv = 1;
% answinv = 1 implies solution where oil inventoires are present  
answlearn = 1;
% answlearn = 1 indicates the usual learning solution. "answlearn" = 0 implies the learning solution used to construct Table 5 

% unpack parameters in *thet* to make them compatible to "sstate"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% exogenous energy shocks

rhoe_p = 0.99999;   % serial correlation of persistent oil-supply shocks
sige_p = thet(1)*0.01; % std. deviation of innovations to those shocks
rhoe_t = thet(2); % serial correlation of transitory oil-supply shocks
sige_t = 0.01;    % std. deviation of innovations to those shocks

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% TFP shocks
rhoz_p = 0.99999;   % serial correlation of TFP shocks
sigz_p = thet(4)*thet(3)*0.01; % std. deviation of innovations to those shocks
rhoz_t = thet(5);   % serial correlation of  TFP shocks
sigz_t = thet(3)*0.01;    % std. deviation of innovations to those shocks

param = [rhoe_p sige_p rhoe_t sige_t rhoz_p sigz_p rhoz_t sigz_t answinv whatinfo answlearn];



gainkeep = []; %this is not used here but in other programs

% Call the program that computes the s.s. and the linearization

comp = [0 1];
track = 0;


lin
system = 'lin';

% Call the KW(2002) program

[M,PI,G]=resolkw(system,track,comp,param,gainkeep);


simule

temp = mean(myoutput);
moments_sim(1,1) = temp(4); % fraction of simulated volatility in spot prices attributable to permanent shocks
                            % as per the Kalmn filter decomposition
                            % discussed in Section 3 of the paper, applied
                            % to the simulated data
temp1 = [temp(1) temp(2) temp(3)];

temp  = mean(sd);
moments_sim(2,1) =  temp(iinv); % s.d. of inventories, relative to s.d. of GDP, simulated and then HP filtered
temp  = mean(cor2);
moments_sim(4,1) =  temp(ispot); % correlation between HP filtered inventories and spot price
temp  = mean(cor1);
moments_sim(5,1) =  temp(iy); % correlation between HP filtered spot price of oil and GDP


moments_sim(3,1) =  mean(autocor_y); % autocorrelation of (HP-filtered) GDP

error = moments_sim-moments;
errorpercent = error ./moments; 

%sumsquares = (error')*poids*error; 
sumsquares = (error./moments)'*poids*(error./moments); 


disp(' ')
disp('parameter vector under consideration:')
disp(thet)

disp('in long format : ')
format long
disp(thet)
format short

disp(' ')
disp('This parameter vector gave sum of squares')
disp(sumsquares)

disp('And here is how well we did in fitting moments')
disp([moments moments_sim error errorpercent])

disp('with average values for phi_tau, sig_tau and sig_p')
disp(temp1)

disp('  ')
disp('******************')


